package com.example.jasonrpc.client;

import com.example.jasonrpc.codec.Decoder;
import com.example.jasonrpc.codec.Encoder;

import java.lang.reflect.Proxy;

public abstract class RpcClient {
    protected Encoder encoder;
    protected Decoder decoder;
    protected TransportSelector selector;

    public RpcClient(ClientConfig config) {
        init(config);
    }

    protected abstract void init(ClientConfig config);

    public <T> T getProxy(String serviceClass) {
        try {
            Class<?> clazz = Class.forName(serviceClass);
            return  (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, new RemoteInvoker(clazz, encoder, decoder, selector));
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Fail to invoke remote service: " + e);
        }
    }
}
